package com.e.ticket;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.e.Dao.UserDao;
import com.e.SQL.UserDBHepler;

public class Update_password extends AppCompatActivity {
    private EditText ed_old,ed_new,ed_re;
    private Button bt_1;
    private UserDao userDao;
    private ImageView iv_1;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update_password);

        ed_old = (EditText) findViewById(R.id.old_pwd);
        ed_new = (EditText) findViewById(R.id.new_pwd);
        ed_re = (EditText) findViewById(R.id.req_pwd);
        bt_1 = (Button) findViewById(R.id.bt_queren);
        this.setTitle("密码修改");
        userDao = new UserDao(new UserDBHepler(Update_password.this));
        Bundle b = getIntent().getExtras();
        String sno = b.getString("sno");
        String password = b.getString("password");

        bt_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String ned = ed_old.getText().toString().trim();
                String pwd = ed_new.getText().toString().trim();
                String req = ed_re.getText().toString().trim();
                if (!"".equals(ned) && !"".equals(pwd) && !"".equals(req)){
                    if (ned.equals(password)){
                        if (pwd.equals(req)){
                            if(userDao.updatePassword(pwd,sno)) {
                                Toast.makeText(Update_password.this,"修改成功",Toast.LENGTH_SHORT).show();
                                startActivity(new Intent(Update_password.this, Login.class));
                                finish();
                            }
                            else
                                Toast.makeText(Update_password.this,"修改失败",Toast.LENGTH_SHORT).show();
                        }
                        else
                            Toast.makeText(Update_password.this,"2次密码不匹配",Toast.LENGTH_SHORT).show();
                    }else{
                        Toast.makeText(Update_password.this,"原密码错误",Toast.LENGTH_SHORT).show();
                    }
                }else{
                    Toast.makeText(Update_password.this,"输入框不能为空",Toast.LENGTH_SHORT).show();
                }
            }
        });

        





    }




    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.back, menu);
        return true;
    }
    //onOptionsItemSelected

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_cart://监听菜单按钮
                Intent intent = new Intent(Update_password.this,MainActivity.class);
                Bundle b = new Bundle();
                b.putInt("ids",3);
                intent.putExtras(b);
                startActivity(intent);
//                Toast.makeText(Update_password.this,"你好",Toast.LENGTH_SHORT).show();
//                Intent intent =new Intent(Update_password.this,Own.class);
//                startActivity(intent);
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}